﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using yncTech.Common;
using yncTech.BusinessObjects;
using System.Data;

public partial class Admin_FinMov_FinMov001 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);
            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["ZYstatus"] != null)
            {
                drpTransferStatus.SelectedValue = Session["ZYstatus"].ToString();
            }
            if (Session["txtTranID"] != null)
            {
                txtTranID.Text = Session["txtTranID"].ToString();
            }
            if (Session["drpTransferType"] != null)
            {
                drpTransferType.SelectedValue = Session["drpTransferType"].ToString();
            }
            if (Session["txtCreator"] != null)
            {
                txtCreator.Text = Session["txtCreator"].ToString();
            }
            if (Session["drpFrom"] != null)
            {
                drpFrom.SelectedValue = Session["drpFrom"].ToString();
            }
            if (Session["drpTo"] != null)
            {
                drpTo.SelectedValue = Session["drpTo"].ToString();
            }
            if (Session["drpThirdStore"] != null)
            {
                drpThirdStore.SelectedValue = Session["drpThirdStore"].ToString();
            }
        }

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["drpThirdStore"] = drpThirdStore.SelectedValue;
        Session["drpTo"] = drpTo.SelectedValue;
        Session["drpFrom"] = drpFrom.SelectedValue;
        Session["txtCreator"] = txtCreator.Text;
        Session["drpTransferType"] = drpTransferType.SelectedValue;
        Session["txtTranID"] = txtTranID.Text;
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["ZYstatus"] = drpTransferStatus.SelectedValue;
    }


    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinShippingQuery q1 = new FinShippingQuery("q1");
        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFThirdUnitQuery q6 = new BFThirdUnitQuery("q6");

        ArrayList args = new ArrayList();
        args.Add(q1.CreatedDttm >= txtStartDate.Value);
        args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        args.Add(q1.ShippingStatus != ConstantValue.ShippingStatusCode.Confirmed);
        if (!string.IsNullOrEmpty(drpTransferStatus.SelectedValue))
        {
            args.Add(q1.ShippingStatus >= drpTransferStatus.SelectedValue);
        }
        q1.InnerJoin(q2).On(q1.ShippingStatus == q2.Code);
        q1.LeftJoin(q3).On(q1.TransferType == q3.Code);
        q1.LeftJoin(q4).On(q1.TransportType == q4.Code);
        q1.LeftJoin(q5).On(q1.FromDestination == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.ToDestination == q6.ThirdUnitID);
        q1.Select(q1.ShipID, q1.CreatedBy, q1.CreatedDttm, q5.Name.As("FromDestination"), q6.Name.As("ToDestination"), q6.ThirdType,
            q1.IsChecked, q1.Check1, q1.Check2, q1.Sender, q1.ShippingDate, q1.ReceivedBy, q1.ShippingStatus.As("ShippingStatusCode"), q1.ReceivedDttm, q1.Comments, q1.AttachmentName, q2.Value1.As("ShippingStatus"), q3.Value1.As("TransferType"), q4.Value1.As("TransportType"))
            .Where(args.ToArray());
        q1.OrderBy(q1.CreatedDttm.Descending);

        if (!string.IsNullOrEmpty(txtTranID.Text))
        {
            q1.Where(q1.ShipID == txtTranID.Text.Substring(4));
        }
        if (!string.IsNullOrEmpty(drpTransferType.SelectedValue))
        {
            q1.Where(q1.TransferType == drpTransferType.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpFrom.SelectedValue))
        {
            q1.Where(q1.FromDestination == drpFrom.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpTo.SelectedValue))
        {
            q1.Where(q1.ToDestination == drpTo.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txtCreator.Text))
        {
            q1.Where(q1.CreatedBy.Like(string.Format("%{0}%",txtCreator.Text)));
        }
        if (!string.IsNullOrEmpty(drpThirdStore.SelectedValue))
        {
            q1.Where(q1.ToDestination == drpThirdStore.SelectedValue);
        }
        DataTable dt = q1.LoadDataTable();
        Business.SetShippingNo(dt);

        pageGrid.DataSource = dt;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("ZYstatus");
        Session.Remove("drpThirdStore");
        Session.Remove("drpTo");
        Session.Remove("drpFrom");
        Session.Remove("txtCreator");
        Session.Remove("drpTransferType");
        Session.Remove("txtTranID");
    }

    protected string GetActionUrl(string status,object oshipId, object rowView)
    {
        string retUrl = "";
        switch (status)
        {
            case ConstantValue.ShippingStatusCode.New:
                retUrl = "<a href='Finmov001g.aspx?type=A&ShipID="+oshipId.ToString()+"' title='添加文档盒' class='aadd'></a>";//+ "添加凭证盒", "aadd", "A", "FinMov001b.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
                retUrl += "   " + GetLinkExt("提交确认", "aAccept", "A", "FinMov001d.aspx", rowView, 680, 380, false, VShippingInfoMetadata.ColumnNames.ShipID);
                retUrl += "   " + GetUpdateLinkExt("FinMov001a.aspx", "aEdit", rowView, 680, 580, VShippingInfoMetadata.ColumnNames.ShipID);
                retUrl += "   " + GetDeleteLinkExt("FinMov001a.aspx", "aDelete", rowView, VShippingInfoMetadata.ColumnNames.ShipID);
                break;
            //case ConstantValue.ShippingStatusCode.Pending:               
                
            //    retUrl = "[ "+GetLink("凭证打包", "A", "FinMov001f.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID)+" ]";
            //    retUrl += " [ " + GetLink("提交复核", "A", "FinMov001e.aspx", rowView, 480, 320, false, VShippingInfoMetadata.ColumnNames.ShipID) + " ]";
            //    break;
            //case ConstantValue.ShippingStatusCode.NeedReply://等待回复
            //    retUrl = GetLink("添加凭证盒", "A", "FinMov001b.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
            //    break;
        }



        //if (status.Equals(ConstantValue.ShippingStatusCode.Pending))
        //{

        //    if (boxCount > 0)
        //    {
        //        retUrl += "   " + GetLink("确认转移", "A", "FinMov001d.aspx", rowView, 980, 580, false, VShippingInfoMetadata.ColumnNames.ShipID);
        //    }


        //}

        return retUrl;
    }



    protected string getBoxCount(object Id, object status)
    {
        string result = "0";
        FinShippingBoxesCollection finShippingBoxesCollection = new FinShippingBoxesCollection();
        finShippingBoxesCollection.Query.Where(finShippingBoxesCollection.Query.ShipID == int.Parse(Id.ToString()));
        finShippingBoxesCollection.Query.Load();

        if (status.ToString() == ConstantValue.ShippingStatusCode.New || status.ToString() == ConstantValue.ShippingStatusCode.Pending)
        {
            result = finShippingBoxesCollection.Count.ToString() + "[<a class='aDocument' title='文档盒' href=\"javascript:OpenWindow('凭证盒','V','/Admin/FinMov/FinMov001b.aspx?type=V&ShipID=" + Id.ToString() + "',1280,680)\"></a>]";
        }
        else
        {
            result = finShippingBoxesCollection.Count.ToString() + "[<a class='aDocument' title='文档盒查看' href=\"FinMov001h.aspx?type=V&returl=FinMov001&ShipID=" + Id.ToString() + "\"></a>]";
        }


        return result;
    }

    protected string getThirdName(object name, object type)
    {
        if (type.ToString() == "JGLX003")
            return name.ToString();
        else
            return "否";
    }
}